﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{

    public class ServiceAccountDayTradeProductAnalysis : Benefit.Interface.Day.IAccountDayTradeProductAnalysis
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountDayTradeProductAnalysis(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 删除某日的所有分析数据
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.AccountDayTradeProductAnalysis where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.AccountDayTradeProductAnalysis d in query)
            {
                db.AccountDayTradeProductAnalysis.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存账号日分析情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitAccountProductDayAnalysis(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            Interface.ServerData.IT_TradeDetail_History tth = new ServerData.ServiceT_TradeDetail_History(db);
            Interface.Sys.IProduct ip = new Sys.ServiceProduct(db);
            Interface.Sys.IAccount iacount = new Sys.ServiceAccount(db);
            Interface.Day.IDayWinOrLoseTick idwinorloset = new Day.ServiceDayWinOrLoseTick(db);
            Interface.Config.IDefaultValue idv = new Config.ServiceDefaultValue(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {

                //  Models.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
                List<string> products = tth.GetAccountDayProduct(pdate, account.Id);
                foreach (string product in products)
                {
                    #region i为0是多，i为1是空
                    for (int i = 0; i < 2; i++)
                    {
                        Models.Day.AccountDayTradeProductAnalysis change = new Models.Day.AccountDayTradeProductAnalysis();
          
                        //  Sys.Account _account = new Sys.Account();
                        change.AccountId = iacount.GetAccountId(account.LoginAccount);
                        change.TradeHistoryId = tradeHistoryId;
                        change.Direction = i;
       
                        change.ProductId = ip.GetProductId(product);
                        change.PingJinPing = tth.GetDayProductPingCount(pdate, account.Id, product, change.Direction.ToString());
                        change.MaxReturn = tth.GetProductMaxReturn(pdate, account.Id, product, change.Direction.ToString());
                        change.AvgPostionTime = -1;
   
                        change.OverStopLoss = idwinorloset.GetProductOverStopLoss(pdate, account.Id, idv.GetOverLostTick(), change.ProductId, change.Direction);
                        change.WinRate = idwinorloset.GetProductWinRate(pdate, account.Id, change.ProductId, change.Direction);
                      //  Interface.ServerData.IT_TradeDetail_History ith
                        double profit = tth.GetDayProductCloseProfit(pdate, account.Id, product, change.Direction.ToString());
                        double fee = tth.GetDayProductCommission(pdate, account.Id, product, change.Direction.ToString());
                        if (fee > 0)
                        {
                            change.Yield = (profit - fee) / fee;
                        }
                        else
                        {
                            change.Yield = 0;
                        }
                        change.RiskCount = -1;
                        change.AvgBillBenefit = tth.GetProductAvgBillBenefit(pdate, account.Id, product, change.Direction.ToString());


                        db.AccountDayTradeProductAnalysis.Add(change);
                    }
                    #endregion
                }
            }
            db.SaveChanges();
        }


        public List<Models.Day.AccountDayTradeProductAnalysis> GetAccountDayTradeProductAnalysis(int accountid, int tradeHistoryId)
        {
            List<Models.Day.AccountDayTradeProductAnalysis> instruments = new List<Models.Day.AccountDayTradeProductAnalysis>();
            var query = db.AccountDayTradeProductAnalysis.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                instruments = query.ToList();
            }
            return instruments;
        }
    }
}